<template>
    <div class="listShop">
<!--        面包屑导航栏-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
<!--            搜索栏-->
            <el-row :gutter="20">
                <el-col :span="10">
                    <el-input placeholder="请输入内容" v-model="queryInfoListshop.query" @clear="getListShopData" clearable>
                        <el-button slot="append" icon="el-icon-search"@click="getListShopData" ></el-button>
                    </el-input>
                </el-col>
                <el-col :span="6">
                    <el-button type="primary" @click="gotoAddListShop">添加商品</el-button>
                </el-col>
            </el-row>
<!--主体表格区域-->
            <el-table
                    :data="listShopData"
                    stripe
                    style="width: 100%">
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column prop="goods_name" label="商品名称" width="700px" ></el-table-column>
                <el-table-column prop="goods_price" label="商品价格(元)" align="center"></el-table-column>
                <el-table-column prop="add_time" label="创建时间" width="200px" align="center">
                    <template v-slot="slot">
                        {{slot.row.add_time|dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column prop="" label="操作"  align="center">
                    <template v-slot="slot">
                        <el-button type="primary" icon="el-icon-edit" size="mini" > </el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteShop(slot.row.goods_id)" > </el-button>
                    </template>
                </el-table-column>
            </el-table>
<!--            分页-->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfoListshop.pagenum"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="queryInfoListshop.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="totals">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "listshop",
        data(){
            return{
                queryInfoListshop:{
                    query:'',
                    pagenum:1,
                    pagesize:10
                },
                listShopData:[],
                totals:0
            }
        },
        created() {
            this.getListShopData();
        },
        methods:{
            async getListShopData(){
                const {data:res}=await this.$http.get("goods",{params:this.queryInfoListshop});
                if(res.meta.status!==200){
                    return this.$message.error("获取数据失败");
                }
                this.listShopData=res.data.goods;
                this.totals=res.data.total;
                // console.log(res);
            },
            handleSizeChange(newsize){
                this.queryInfoListshop.pagesize=newsize;
                this.getListShopData();
            },
            handleCurrentChange(newnum){
                this.queryInfoListshop.pagenum=newnum;
                this.getListShopData();
            },
            async deleteShop(id){
                const confirmResult=await this.$confirm("此操作将永久删除该商品，是否继续？","提示",{
                    confirmButtonText:"确定",
                    cancelButtonText:"取消",
                    type:'warning'
                }).catch(err=>err);
                if(confirmResult!=="confirm"){
                    return this.$message.info("已经取消删除");
                }
                else {
                    const {data:res}=await this.$http.delete(`goods/${id}`);
                    // console.log(res);
                    if(res.meta.status!==200){
                        return this.$message.error("删除失败");
                    }
                    this.$message.success("删除成功");
                    this.getListShopData();
                }
            },
            gotoAddListShop(){
                this.$router.push('/goods/add')
            }

        }
    }
</script>

<style lang="less" scoped>
    .listShop{
        padding: 15px;
    }
    .el-card{
        margin-top: 15px;
    }
</style>
